package com.ujiuye.dao;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.ujiuye.pojo.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class HomeworkDao {
    private QueryRunner qr = new QueryRunner(new ComboPooledDataSource());
    //验证账号唯一
    public int validUname(String uname) {
        String sql = "select count(*) from user where uname = ?";
        try {
            long l = (long)qr.query(sql,new ScalarHandler(),uname);
            return (int)l;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;

    }

    //模糊查询
    public List<User> dimQuery(String uname) {

        String sql = "select * from user where uname like ?";
        try {
            return qr.query(sql,new BeanListHandler<>(User.class),uname + "%");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
